﻿//---------------------------------------------------------------------------------
// Copyright (C) 2008 Chillisoft Solutions
// 
// This file is part of the Habanero framework.
// 
//     Habanero is a free framework: you can redistribute it and/or modify
//     it under the terms of the GNU Lesser General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     The Habanero framework is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU Lesser General Public License for more details.
// 
//     You should have received a copy of the GNU Lesser General Public License
//     along with the Habanero framework.  If not, see <http://www.gnu.org/licenses/>.
//---------------------------------------------------------------------------------

using Firestarter.Test.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestSuperClassLookupComboBoxMapper
    {
        [SetUp]
        public void Setup()
        {
            BORegistry.DataAccessor = new DataAccessorInMemory();
            BusinessObjectManager.Instance.ClearLoadedObjects();
            BusinessObjectManager.Instance.ClearLoadedObjects();
            TestUtilsDomain.WaitForGC();
            UITestUtils.SetupTest();
        }

        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        private static IControlFactory GetControlFactory()
        {
            ControlFactoryWin factory = new ControlFactoryWin();
            GlobalUIRegistry.ControlFactory = factory;
            return factory;
        }

        [Test]
        public void Test_Construct()
        {
            //--------------- Set up test pack ------------------
            IControlFactory controlFactory = GetControlFactory();
            ComboBoxWin superClassComboBox = new ComboBoxWin();
            string propName = TestUtilsShared.GetRandomString();
            //--------------- Test Preconditions ----------------

            //--------------- Execute Test ----------------------
            SuperClassLookupComboBoxMapper mapper = new SuperClassLookupComboBoxMapper(superClassComboBox, propName, false, controlFactory);
            //--------------- Test Result -----------------------
            Assert.IsInstanceOf(typeof(IControlMapper), mapper);
            Assert.AreSame(superClassComboBox, mapper.Control);
            Assert.AreEqual(propName, mapper.PropertyName);
            Assert.AreEqual(false, mapper.IsReadOnly);
            Assert.AreEqual(controlFactory, mapper.ControlFactory);
            SuperClassLookupComboBoxMapper lookupComboBoxMapper = mapper;
            Assert.IsNotNull(lookupComboBoxMapper);
            Assert.AreSame(superClassComboBox, lookupComboBoxMapper.Control);
            Assert.AreEqual(propName, lookupComboBoxMapper.PropertyName);
            Assert.AreEqual(false, lookupComboBoxMapper.IsReadOnly);
            Assert.AreEqual(controlFactory, lookupComboBoxMapper.ControlFactory);
            Assert.AreEqual(lookupComboBoxMapper.ErrorProvider, mapper.ErrorProvider);
        }

        [Test]
        public void Test_Construct_ReadOnly()
        {
            //--------------- Set up test pack ------------------
            IControlFactory controlFactory = GetControlFactory();
            ComboBoxWin superClassComboBox = new ComboBoxWin();
            string propName = TestUtilsShared.GetRandomString();
            //--------------- Test Preconditions ----------------

            //--------------- Execute Test ----------------------
            SuperClassLookupComboBoxMapper mapper = new SuperClassLookupComboBoxMapper(
                superClassComboBox, propName, true, controlFactory);
            //--------------- Test Result -----------------------
            Assert.IsInstanceOf(typeof(IControlMapper), mapper);
            Assert.AreEqual(true, mapper.IsReadOnly);
            SuperClassLookupComboBoxMapper lookupComboBoxMapper = mapper;
            Assert.IsNotNull(lookupComboBoxMapper);
            Assert.AreEqual(true, lookupComboBoxMapper.IsReadOnly);
        }

        [Test]
        public void Test_SetBusinessObject()
        {
            //--------------- Set up test pack ------------------
            SuperClassLookupComboBoxMapper mapper = CreateSuperClassLookupComboBoxMapper("TestProp");
            //--------------- Test Preconditions ----------------
            Assert.IsNull(mapper.BusinessObject);
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            //--------------- Execute Test ----------------------
            mapper.BusinessObject = dmInheritanceRelationship;
            //--------------- Test Result -----------------------
            Assert.AreSame(dmInheritanceRelationship, mapper.BusinessObject);
        }

        [Test]
        public void Test_WhenBusinessObjectSet_ShouldExcludeSelf()
        {
            //--------------- Set up test pack ------------------

            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            DMClass subClass = TestUtilsDMClass.CreateSavedDMClass();
            dmInheritanceRelationship.SubClass = subClass;

            TestUtilsDMClass.CreateSavedDMClass();
            TestUtilsDMClass.CreateSavedDMClass();

            IControlFactory controlFactory = GetControlFactory();
            ComboBoxWin extendedComboBox = new ComboBoxWin();
            const string propName = "SuperClassID";
            SuperClassLookupComboBoxMapper mapper = new SuperClassLookupComboBoxMapper(extendedComboBox, propName, true, controlFactory);
            //--------------- Test Preconditions ----------------
            Assert.IsNull(mapper.BusinessObject);
            //--------------- Execute Test ----------------------
            mapper.BusinessObject = dmInheritanceRelationship;
            //--------------- Test Result -----------------------
            Assert.AreSame(dmInheritanceRelationship, mapper.BusinessObject);
            Assert.AreEqual(2, mapper.LookupList.Count);
        }
        
        [Test]
        public void Test_WhenBusinessObjectSet_ShouldExcludeSelfAndSubClasses()
        {
            //--------------- Set up test pack ------------------

            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            DMClass subClass = TestUtilsDMClass.CreateSavedDMClass();
            dmInheritanceRelationship.SubClass = subClass;
            dmInheritanceRelationship.SuperClass = TestUtilsDMClass.CreateSavedDMClass();
            
            DMInheritanceRelationship dmSubClassRelationship = new DMInheritanceRelationship();
            DMClass subsubClass = TestUtilsDMClass.CreateSavedDMClass();
            dmSubClassRelationship.SubClass = subsubClass;
            dmSubClassRelationship.SuperClass = subClass;

            TestUtilsDMClass.CreateSavedDMClass();
            TestUtilsDMClass.CreateSavedDMClass();

            IControlFactory controlFactory = GetControlFactory();
            ComboBoxWin extendedComboBox = new ComboBoxWin();
            dmInheritanceRelationship.ClassDef.PropDefcol["SuperClassID"].LookupList.TimeOut = 0;
            const string propName = "SuperClassID";
            SuperClassLookupComboBoxMapper mapper = new SuperClassLookupComboBoxMapper(extendedComboBox, propName, true, controlFactory);
            
            //--------------- Test Preconditions ----------------
            Assert.IsNull(mapper.BusinessObject);
            Assert.AreEqual(1, subClass.SubClassRelationships.Count);
            //--------------- Execute Test ----------------------
            mapper.BusinessObject = dmInheritanceRelationship;
            //--------------- Test Result -----------------------
            Assert.AreSame(dmInheritanceRelationship, mapper.BusinessObject);
            Assert.AreEqual(3, mapper.LookupList.Count);
        }

        [Test]
        public void Test_WhenBusinessObjectNull_ShouldNotRaiseError()
        {
            //--------------- Set up test pack ------------------
            IControlFactory controlFactory = GetControlFactory();
            ComboBoxWin extendedComboBox = new ComboBoxWin();
            const string propName = "SuperClassID";
            SuperClassLookupComboBoxMapper mapper = new SuperClassLookupComboBoxMapper(extendedComboBox, propName, true, controlFactory);
            //--------------- Test Preconditions ----------------
            Assert.IsNull(mapper.BusinessObject);

            //--------------- Execute Test ----------------------
            mapper.BusinessObject = null;
            //--------------- Test Result -----------------------
            Assert.AreEqual(0, mapper.LookupList.Count);
        }
        [Test]
        public void Test_WhenBusinessObjectNull_ShouldNotClearList()
        {
            //--------------- Set up test pack ------------------
            TestUtilsDMClass.CreateSavedDMClass();
            TestUtilsDMClass.CreateSavedDMClass();
            IControlFactory controlFactory = GetControlFactory();
            ComboBoxWin extendedComboBox = new ComboBoxWin();
            const string propName = "SuperClassID";
            SuperClassLookupComboBoxMapper mapper = new SuperClassLookupComboBoxMapper(extendedComboBox, propName, true, controlFactory);
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            dmInheritanceRelationship.SubClass = TestUtilsDMClass.CreateSavedDMClass();
            mapper.BusinessObject = dmInheritanceRelationship;
            //--------------- Test Preconditions ----------------
            Assert.AreEqual(2, mapper.LookupList.Count);
            //--------------- Execute Test ----------------------
            mapper.BusinessObject = null;
            //--------------- Test Result -----------------------
            Assert.AreEqual(2, mapper.LookupList.Count);
        }

        [Test]
        public void Test_SetBusinessObject_OnInternalLookupComboBoxMapper()
        {
            //--------------- Set up test pack ------------------
            SuperClassLookupComboBoxMapper mapper = CreateSuperClassLookupComboBoxMapper("SuperClassID");
            //--------------- Test Preconditions ----------------
            Assert.IsNull(mapper.BusinessObject);
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            //--------------- Execute Test ----------------------
            mapper.BusinessObject = dmInheritanceRelationship;
            //--------------- Test Result -----------------------
            Assert.AreSame(dmInheritanceRelationship, mapper.BusinessObject);
        }

        [Test]
        public void Test_WhenSubClassInInheritanceRelationship_ShouldNotRaiseError()
        {
            //---------------Set up test pack-------------------
            SuperClassLookupComboBoxMapper mapper = CreateSuperClassLookupComboBoxMapper("SuperClassID");
            DMProperty parentDiscriminatorProp = TestUtilsShared.CreateSavedDMProperty();
            DMClass parentClass = (DMClass) parentDiscriminatorProp.Class;

            DMInheritanceRelationship childParentRelationship = new DMInheritanceRelationship();
            
            DMClass childClass = TestUtilsShared.CreateSavedDMClass(parentClass.Assembly);
            childParentRelationship.SubClass = childClass;
            childParentRelationship.SuperClass = parentClass;
            childParentRelationship.Discriminator = parentDiscriminatorProp.PropertyName;

            DMInheritanceRelationship grandChildInheritanceRelationship = new DMInheritanceRelationship();
            grandChildInheritanceRelationship.SubClass = null;
            grandChildInheritanceRelationship.SuperClass = childClass;

            TestUtilsShared.CreateSavedDMClass(parentClass.Assembly);
            //---------------Assert Precondition----------------
            Assert.IsNull(grandChildInheritanceRelationship.SubClass);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = childParentRelationship;
            //---------------Test Result -----------------------
            Assert.AreEqual(2,mapper.LookupList.Count, "The parent class and additional classes are available");
        }

        private static SuperClassLookupComboBoxMapper CreateSuperClassLookupComboBoxMapper(string propertyName)
        {
            IControlFactory controlFactory = GetControlFactory();
            ComboBoxWin extendedComboBox = new ComboBoxWin();
            SuperClassLookupComboBoxMapper mapper = new SuperClassLookupComboBoxMapper(extendedComboBox, propertyName, true, controlFactory);
            return mapper;
        }

    }
}